from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import text

from app.core.database import get_db
from app.utils.redis_client import redis_client

router = APIRouter()


@router.get("/")
async def health_check():
    """
    Basic health check endpoint
    """
    return {
        "status": "healthy",
        "message": "FastAPI application is running"
    }


@router.get("/detailed")
async def detailed_health_check(db: AsyncSession = Depends(get_db)):
    """
    Detailed health check including database and Redis status
    """
    status = {"status": "healthy", "checks": {}}
    
    # Check database connection
    try:
        await db.execute(text("SELECT 1"))
        status["checks"]["database"] = {"status": "healthy"}
    except Exception as e:
        status["status"] = "unhealthy"
        status["checks"]["database"] = {
            "status": "unhealthy",
            "error": str(e)
        }
    
    # Check Redis connection
    try:
        if not redis_client.redis:
            await redis_client.init_redis_pool()
        await redis_client.redis.ping()
        status["checks"]["redis"] = {"status": "healthy"}
    except Exception as e:
        status["status"] = "unhealthy"
        status["checks"]["redis"] = {
            "status": "unhealthy", 
            "error": str(e)
        }
    
    return status